package com.cgbl.cg.ui.course

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.CourseProfileCommentAdapter
import com.cgbl.cg.adapter.PhotoAdapter
import com.cgbl.cg.databinding.AppActivityCommentInputBinding
import com.cgbl.cg.databinding.AppActivityCommentProfileBinding
import com.cgbl.cg.view.ImagePop
import com.cgbl.cg.vm.course.CommentInputViewModel
import com.cgbl.cg.vm.course.CommentProfileViewModel
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.util.ChoiceImgHelper

@Route(path = AppConstants.Router.Mine.A_MINE_COMMENT_INPUT)
class CommentInputActivity :
    GLBaseActivity<AppActivityCommentInputBinding, CommentInputViewModel>() {

    private var productId = ""
    private var commentType = 0
    private var parentId = ""
    private var accountId = ""
    private lateinit var photoAdapter: PhotoAdapter
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_comment_input

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initTiterBar()
        initAdapter()
        viewModel.initParams(commentType, productId, parentId, accountId)
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        val rightView = findViewById<TextView>(R.id.tv_right_text)
        rightView.visibility = View.VISIBLE
        rightView.text = "提交"
        titleView.text = ""
        backView.setOnClickListener { finish() }
        rightView.setOnClickListener {
            viewModel.submitComment()
        }
    }

    override fun initParam() {
        commentType = intent.getIntExtra(
            AppConstants.BundleKey.COMMENT_PRODUCT_TYPE,
            AppConstants.BundleValue.COMMENT_TYPE_COURSE
        )
        productId = intent.getStringExtra(AppConstants.BundleKey.COMMENT_PRODUCT_ID) ?: ""
        parentId = intent.getStringExtra(AppConstants.BundleKey.COMMENT_PARENT_ID) ?: ""
        accountId = intent.getStringExtra(AppConstants.BundleKey.COMMENT_ACCOUNT_ID) ?: ""
    }

    private fun initAdapter() {
        photoAdapter = PhotoAdapter(R.mipmap.app_ic_photo_add_a7a7a7)
        binding.mRvImg.apply {
            adapter = photoAdapter
        }
        photoAdapter.addData("")
        photoAdapter.addChildClickViewIds(R.id.item_iv_img, R.id.item_iv_close)

        photoAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_iv_img -> {
                    if (TextUtils.isEmpty(photoAdapter.data[position])) {
                        ChoiceImgHelper.ChoiceImg(this, 9, {
                            it.forEach {
                                viewModel.doUpload(it?.fileName ?: "", it?.compressPath ?: "") {
                                    photoAdapter.addData(0, it)
                                }

                            }
                        }, {

                        })
                    } else {
                        val list = mutableListOf<String>()
                        photoAdapter.data.forEach {
                            if (!TextUtils.isEmpty(it)) list.add(it)
                        }
                        val imgPop = ImagePop(this).isInfinite(true).isShowSaveButton(false)
                            .setXPopupImageLoader(SmartGlideImageLoader())
                            .setSrcView(view.findViewById(R.id.item_iv_img), position)
                            .setImageUrls(list.toMutableList())
                        XPopup.Builder(this).asCustom(imgPop).show()
                    }
                }
                R.id.item_iv_close -> {
                    viewModel.imgList.remove(photoAdapter.data[position])
                    photoAdapter.data.removeAt(position)
                }
            }
        }
    }
}